package com.jiantong.web.open.app.work;

import com.jiantong.user.service.BreedFactoryReadService;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.FeedReport;
import com.jiantong.work.service.FeedReportReadService;
import com.jiantong.work.service.FeedReportWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.Map;

/**
 *@author 毛文杰
 * 养殖端----生产中心---
 */
@Slf4j
@OpenBean
public class FeedReportApis {
    @RpcConsumer
    private FeedReportReadService feedReportReadService;
    @RpcConsumer
    private FeedReportWriteService feedReportWriteService;

    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;

    @OpenMethod(key = "find.feed.report.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public FeedReport findById(Long id){
        if(id == null){
            log.error("id should be not bull");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not bull");
        }
        Response<FeedReport> resp = feedReportReadService.findById(id);
        if (!resp.isSuccess()){
            log.error("failed to find feed report  by id = {}, cause : {}", id, resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }


    @OpenMethod(
            key = "paging.feed.report",
            paramNames = {"pageNo", "pageSize","status","breedFactoryId"},
            httpMethods = RequestMethod.GET)
    public Paging<FeedReport> pagingFeedReport(Integer pageNo, Integer pageSize,
                                               Integer status,Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "breedFactoryId should be not null");
        }
        Response<Paging<FeedReport>> resp =
                feedReportReadService.pagingFeedReport(pageNo, pageSize, status,breedFactoryId);
        if (!resp.isSuccess()){
            log.error("failed to paging feed report by id = {}, cause : {}", status, resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }

    /**
     * 饲料中心 政府端时间查询
     * todo
     * @return status =1
     */
    @OpenMethod(
            key = "paging.status.feed.report",
            paramNames = {"pageNo", "pageSize", "breedFactoryId","breedType","fromDate", "toDate"},
            httpMethods = RequestMethod.GET)
    public Paging<FeedReport> pagingFeedReports(Integer pageNo,
                                                Integer pageSize,
                                                Long breedFactoryId,
                                                Integer feedType,
                                                String fromDate,
                                                String toDate) {
        Date feedReportStart = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            feedReportStart = DateTime.parse(fromDate).toDate();
        }

        Date feedReportEnd = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            feedReportEnd = DateTime.parse(toDate).toDate();
        }
        Response<Paging<FeedReport>> resp = feedReportReadService
                .pagingFeedReports (pageNo, pageSize, breedFactoryId,feedType, feedReportStart, feedReportEnd);
        if (!resp.isSuccess()) {
            log.error("failed to paging feed report{}, cause : {}", resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();

    }


    @OpenMethod(key = "create.feed.report",
            paramNames = {"feedReport","toStatus"},
            httpMethods = RequestMethod.POST)
    public Map<String,Object> create(FeedReport feedReport,Integer toStatus){
        Response<Map<String,Object>> response = feedReportWriteService.create(feedReport,toStatus);
        if(!response.isSuccess()){
        log.error("failed to create feed report  by feedReport = {}, cause : {}", feedReport,toStatus, response.getError());
        throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),
                response.getError());
    }
        return response.getResult();
}

    @OpenMethod(key = "update.feed.report",
            paramNames = {"feedReport","toStatus"},
            httpMethods=RequestMethod.PUT)
    public Map<String,Object> update(FeedReport feedReport,Integer toStatus){
        if(feedReport.getId() == null){
            log.error("id should not null  cause id = : {}",feedReport.getId());
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should not null");
        }
        Response<Map<String , Object>> statusResponse = feedReportReadService.findStatusById(feedReport.getId());
        if(statusResponse.isSuccess() && !statusResponse.getResult().get("status").equals(feedReport.getStatus())){
            log.error("feedReport is already submitted error = {}, cause : {}", feedReport.getStatus(),statusResponse.getResult().get("status"));
            throw new OPClientException(ExceptionCode.DOCUMENT_REPEAT_SUBMIT.value(),
                    "this.report.is.already.submitted");
        }

        Response<Map<String,Object>> response = feedReportWriteService.update(feedReport,toStatus);
        if(!response.isSuccess()){
            log.error("failed to update  feed report by feedReport = {}, cause : {}", feedReport, response.getError());
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    response.getError());
        }
        return response.getResult();
    }
}
